﻿using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;


namespace SharpMedia.Pages
{
    public class StopMenuPage : MediaPage
    {
        public bool EndOfTheLine { get; set; }
        public VideoList VideoList { get; set; }
        public event EventHandler StopPressed;
        public event EventHandler PlayPressed;
        public event EventHandler FileSwitched;

        private HUDLabel lblStopPrev, lblStopNext, lblStopPlay, lblStopStop;
        private HUDImage stopMenuBack, imgStopNext, imgStopPrev, imgStopStop, imgStopPlay;

        private HUDContainerSimple stopMenu;


        public StopMenuPage(VideoList videoList, bool end, HUDPage parent) : base(parent)
        {
            this.VideoList = videoList;
            this.EndOfTheLine = end;
            stopMenu = new HUDContainerSimple();
            stopMenu.Size = Size;
            stopMenu.Anchor = Anchor.All;
            Add(stopMenu);

            stopMenuBack = new HUDImage("white.png");
            stopMenuBack.ConstrainProportions = false;
            stopMenuBack.Color = Lizk.SimpleHUD.ColorS.Black;
            stopMenuBack.Bounds = Bounds;
            stopMenuBack.Alpha = 127;
            stopMenuBack.Anchor = Anchor.All;
            stopMenu.Add(stopMenuBack);

            imgStopPrev = new HUDImage("pb_previous140.png");
            imgStopPrev.Size = new SizeS(140, 140);
            stopMenu.Add(imgStopPrev);

            imgStopPlay = new HUDImage("pb_play140.png");
            imgStopPlay.Size = new SizeS(140, 140);
            stopMenu.Add(imgStopPlay);

            imgStopStop = new HUDImage("pb_stop140.png");
            imgStopStop.Size = new SizeS(140, 140);
            stopMenu.Add(imgStopStop);

            imgStopNext = new HUDImage("pb_next140.png");
            imgStopNext.Size = new SizeS(140, 140);
            stopMenu.Add(imgStopNext);

            lblStopNext = new HUDLabel("Next episode", "tahoma38b");
            lblStopNext.Size = new SizeS(220, 50);
            lblStopNext.Alignment = Alignment.Center | Alignment.Top;
            stopMenu.Add(lblStopNext);

            lblStopPlay = new HUDLabel("Resume episode", "tahoma38b");
            lblStopPlay.Size = new SizeS(220, 50);
            lblStopPlay.Alignment = Alignment.Center | Alignment.Top;
            stopMenu.Add(lblStopPlay);

            lblStopPrev = new HUDLabel("Previous episode", "tahoma38b");
            lblStopPrev.Size = new SizeS(320, 50);
            lblStopPrev.Alignment = Alignment.Center | Alignment.Top;
            stopMenu.Add(lblStopPrev);

            lblStopStop = new HUDLabel("Return to menu", "tahoma38b");
            lblStopStop.Size = new SizeS(320, 50);
            lblStopStop.Alignment = Alignment.Center | Alignment.Top;
            stopMenu.Add(lblStopStop);

            SetStopMenuPos();

            imgStopPrev.Visible = lblStopPrev.Visible = videoList.SelectedIndex > 0;
            imgStopNext.Visible = lblStopNext.Visible = videoList.SelectedIndex < videoList.Count - 1;
            lblStopPrev.Text = "Previous file";
            lblStopNext.Text = "Next file";

            

            lblStopPlay.Text = end ? "Restart " + videoList.SelectedItem.Name : "Resume";
        }


        private void SetStopMenuPos()
        {
            imgStopPlay.Location = new PointS(Bounds.Width / 2f - 70, Bounds.Height / 2f - 175);
            lblStopPlay.Location = new PointS(imgStopPlay.Bounds.Left - 40, imgStopPlay.Bounds.Bottom + 10);
            imgStopPrev.Location = new PointS(imgStopPlay.Bounds.Left - 320, imgStopPlay.Bounds.Top);
            lblStopPrev.Location = new PointS(imgStopPrev.Bounds.Left - 90, imgStopPrev.Bounds.Bottom + 10);
            imgStopNext.Location = new PointS(imgStopPlay.Bounds.Left + 320, imgStopPlay.Bounds.Top);
            lblStopNext.Location = new PointS(imgStopNext.Bounds.Left - 40, imgStopNext.Bounds.Bottom + 10);
            imgStopStop.Location = new PointS(imgStopPlay.Bounds.Left, imgStopPlay.Bounds.Bottom + 70);
            lblStopStop.Location = new PointS(imgStopStop.Bounds.Left - 90, imgStopStop.Bounds.Bottom + 10);
        }

        public override void ResizeMove(RectangleS oldRect, RectangleS newRect)
        {
            if (imgStopPlay != null)
            {
                SetStopMenuPos();
            }
            base.ResizeMove(oldRect, newRect);
        }

        public override void Close()
        {
            Fade(0, 6f);
            base.Close();
        }

        public override void Show()
        {
            Fade(255, 6f);
            base.Show();
        }

        public override void Command(HUDCommands command, object tag)
        {
            switch (command)
            {
                case HUDCommands.Previous:
                    if (VideoList.SelectedIndex > 0)
                    {
                        VideoList.SelectedIndex--;
                        if (FileSwitched != null)
                            FileSwitched(this, EventArgs.Empty);
                        SharpMedia.Back();
                    }
                    break;
                case HUDCommands.Next:
                    if (VideoList.SelectedIndex < VideoList.Count - 1)
                    {
                        VideoList.SelectedIndex++;
                        if (FileSwitched != null)
                            FileSwitched(this, EventArgs.Empty);
                        SharpMedia.Back();
                    }
                    break;

                case HUDCommands.Back:
                case HUDCommands.Stop:
                    if (StopPressed != null)
                        StopPressed(this, EventArgs.Empty);
                    SharpMedia.Back();
                    break;

                case HUDCommands.Play:
                case HUDCommands.Pause:
                case HUDCommands.PlayPause:
                    if (PlayPressed != null)
                        PlayPressed(this, EventArgs.Empty);
                    SharpMedia.Back();
                        break;
                default:
                    break;
            }
        }
    }
}
